<?php
define('ACTION_ADDENTRY2DESTINY', 'add');
define('ACTION_ADDFEELING2ENTRY', 'addf');
define('ACTION_ADDTEXT2ENTRY', 'addt');
define('ACTION_ADDIMAGE2ENTRY', 'addi');
define('ACTION_ENTRYFINISHED', 'finish');
define('ACTION_ADDCOMMENT2ENTRY', 'addc');
define('ACTION_DELETEENTRY4DESTINY', 'del');
define('ACTION_LISTENTRY', 'liste');

switch(true){
	case (strcmp($action, ACTION_ADDENTRY2DESTINY) == 0):
		if(isset($_GET['gid']) && isset($_GET['pid']) && isset($_GET['feeling'])){
			$idGroup = $_GET['gid'];
			$idPlace = $_GET['pid'];
			$text = $_POST['text'];
			$feeling = $_GET['feeling'];
			
			$group = new Group();
			$place = new Place();
			
			if($group->loadFromDB($idGroup)){ //Carga y comprueba el grupo
				if($place->loadFromDB($idPlace)){ //Carga y comprueba el lugar.
					if($user->isInvitedInGroup($idGroup)){
						if($user->isPlaceInGroup($idPlace, $idGroup)){
							$entry = $user->addNewEntry($idGroup, $idPlace, $text, $feeling);
							$result = array('status'=> NO_ERR, 'result' => $entry->tojson());
						} else {
							$result = array('status' => ERR_NOT_PLACE_INGROUP);
						}
					} else {
						$result = array('status' => ERR_NOTINGROUP);
					}
				} else {
					$result = array('status' => ERR_PLACE_NOTEXISTS);
				}
			} else {
				$result = array('status' => ERR_GROUP_NOTEXISTS);
			}
		} else {
			$result = array('status' => ERR_BAD_PARAMS);			
		}		
		break;
	case (strcmp($action, ACTION_DELETEENTRY4DESTINY) == 0):
		if(isset($_GET['eid'])){
			$idEntry = $_GET['eid'];
			$entry = new Entry();
			if($entry->loadFromDB($idEntry)){
				if($entry->isAutorEntry($user->getId())){
					$user->deleteEntry($idEntry);
					$result = array('status'=> NO_ERR);
				} else {
					$result = array('status' => ERR_NOTAUTOR_ENTRY);
				}
			} else {
				$result = array('status' => ERR_ENTRY_NOTEXISTS);
			}
		} else {
			$result = array('status' => ERR_BAD_PARAMS);			
		}				
		break;
	case (strcmp($action, ACTION_LISTENTRY) == 0):
		$entries = $user->getEntries();
		$result = array();
		for ($i=0; $i < count($entries) ; $i++) { 
			$result[$i] = $entries[$i]->tojson();
		}		
		$result = array('status'=> NO_ERR, 'result'=> $result);
		break;
	case (strcmp($action, ACTION_ADDFEELING2ENTRY) == 0):
		if(isset($_GET['eid'])){
			$idEntry = $_GET['eid'];
			$feeling = $_GET['feeling'];
			
			$entry = new Entry();
			
			if($entry->loadFromDB($idEntry)){
				if($entry->isAutorEntry($user->getId())){
					$entry->addFeeling2Entry($feeling);
					$result = array('status'=> NO_ERR, 'result' => $entry->tojson());
				} else {
					$result = array('status' => ERR_NOTAUTOR_ENTRY);
				}
			} else {
				$result = array('status' => ERR_ENTRY_NOTEXISTS);
			}
		} else {
			$result = array('status' => ERR_BAD_PARAMS);			
		}	
		break;
	case (strcmp($action, ACTION_ADDTEXT2ENTRY) == 0):
		if(isset($_GET['eid'])){
			$idEntry = $_GET['eid'];
			$text = $_POST['text'];
			
			$entry = new Entry();
			
			if($entry->loadFromDB($idEntry)){
				if($entry->isAutorEntry($user->getId())){
					$entry->addText2Entry($text);
					$result = array('status'=> NO_ERR, 'result' => $entry->tojson());
				} else {
					$result = array('status' => ERR_NOTAUTOR_ENTRY);
				}
			} else {
				$result = array('status' => ERR_ENTRY_NOTEXISTS);
			}
		} else {
			$result = array('status' => ERR_BAD_PARAMS);			
		}	
		break;
	case (strcmp($action, ACTION_ADDIMAGE2ENTRY) == 0):
		if(isset($_FILES['image']) && isset($_GET['eid'])){
			$idEntry = $_GET['eid'];
			$tiposPermitidos = array('image/gif','image/jpeg','image/png');
			define('MAX_SIZE', '5324062');
			
			$entry = new Entry();
			if($entry->loadFromDB($idEntry)){
				if($entry->isAutorEntry($user->getId())){
					$directorio = "img/";
					$dirUser = $directorio.$user->getId();
					
					if(!file_exists($dirUser)) mkdir($dirUser, 0777);
					$filename = $dirUser.'/'.time().image_type_or_mime_type_to_extension($_FILES['image']['type']);
					if($_FILES['image']['size'] <= MAX_SIZE){
						if(in_array($_FILES['image']['type'], $tiposPermitidos)){
							move_uploaded_file($_FILES['image']['tmp_name'],$filename);
							$entry->addNewImage2Entry($filename);
							if(!is_uploaded_file($_FILES['image']['tmp_name'])){
								$result = array('status'=> NO_ERR, 'result' => $entry->tojson());
							}
						} else {
							$result = array('status' => ERR_IMAGE_TYPE);
						}
					} else {
						$result = array('status' => ERR_IMAGE_SIZE);
					}
				} else {
					$result = array('status' => ERR_NOTAUTOR_ENTRY);
				}
			} else {
				$result = array('status' => ERR_ENTRY_NOTEXISTS);
			}
			
			
		} else {
			$result = array('status' => ERR_BAD_PARAMS);			
		}	
		break;
	case (strcmp($action, ACTION_ENTRYFINISHED) == 0):
		if(isset($_GET['eid'])){
			$idEntry = $_GET['eid'];
			
			$entry = new Entry();
			
			if($entry->loadFromDB($idEntry)){
				if($entry->isAutorEntry($user->getId())){
					$entry->finish();
					$result = array('status'=> NO_ERR, 'result' => $entry->tojson());
				} else {
					$result = array('status' => ERR_NOTAUTOR_ENTRY);
				}
			} else {
				$result = array('status' => ERR_ENTRY_NOTEXISTS);
			}
		} else {
			$result = array('status' => ERR_BAD_PARAMS);			
		}	
		break;
	case (strcmp($action, ACTION_ADDCOMMENT2ENTRY) == 0):
		if(isset($_GET['eid'])){
			$idEntry = $_GET['eid'];
			$comment = $_POST['comment'];
			
			$entry = new Entry();
			
			if($entry->loadFromDB($idEntry)){
				$entry->addComment2Entry($comment, $user->getId());
				$result = array('status'=> NO_ERR, 'result' => $entry->tojson());
			} else {
				$result = array('status' => ERR_ENTRY_NOTEXISTS);
			}
		} else {
			$result = array('status' => ERR_BAD_PARAMS);			
		}	
		break;
	default:
		$result = array('status' => ERR_GENERAL);
}

?>